package com.generic;

import java.util.*;

/**
 * @author Vhong
 * @version 1.0
 */
public class GenericExercise {
    public static void main(String[] args) {
       //使用泛型方式给HashSet放入3个学生对象
        //1.HashSet
        HashSet<Student> students = new HashSet<Student>();
        students.add(new Student("jack",19));
        students.add(new Student("mary",20));
        students.add(new Student("smith",18));

        //遍历
        for(Student student : students){
            System.out.println(student);
        }
        //使用泛型方式给HashMap 放入3个学生对象
        //2.HashMap
        HashMap<String,Student> hm = new HashMap<String,Student>();
        hm.put("tom",new Student("tom",38));
        hm.put("milan",new Student("milan",48));
        hm.put("hsp",new Student("hsp",28));

        //迭代器 EntrySet
        /*
          public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
           }
         */
        System.out.println("========迭代器==========");
        Set<Map .Entry<String,Student>> entries = hm.entrySet();
        Iterator<Map.Entry<String, Student>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Student> next =  iterator.next();
            System.out.println(next.getKey()+" - "+next.getValue());
        }
    }
}
class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}